package com.solution.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.solution.constant.LoginConstants;
import com.solution.delegate.UserInfoServiceDelegate;
import com.solution.form.ChangePasswordForm;
import com.solution.model.UserInfo;

public class AdminChangePassword extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
	{
	    Logger logger = Logger.getLogger(AdminChangePassword.class.getName());
	    ChangePasswordForm changePasswordForm = (ChangePasswordForm)form;
	    HttpSession session=request.getSession();
	    UserInfo userInfo=(UserInfo)session.getAttribute(LoginConstants.SESSION_USER_INFO);
	    String changeOption=request.getParameter("changemode");

	    if("change".equalsIgnoreCase(changeOption) || "ageExpire".equalsIgnoreCase(changeOption) )
        {
	    	changePasswordForm.setUserID(userInfo.getUsername());
	    	if("ageExpire".equalsIgnoreCase(changeOption)){
	    		changePasswordForm.setLogout("Y");
	    	}	    	

	        return mapping.findForward("success");
        }
	    else if("firstLogin".equalsIgnoreCase(changeOption)){
	    	changePasswordForm.setUserID(userInfo.getUsername());
	    	changePasswordForm.setLogout("Y");
	    	return mapping.findForward("firstLogin");
	    }
	    else if ("chng_password".equalsIgnoreCase(changeOption))
	    {

		    try{
		    	UserInfoServiceDelegate userInfoService = new UserInfoServiceDelegate();
		    	UserInfo userInfo_validate = new UserInfo();
		    	userInfo_validate.setUsername(userInfo.getUsername());
		    	userInfo_validate.setPassword(userInfoService.encryptPassword(changePasswordForm.getOldPassword()));
		          if(userInfoService.validateUserPassword(userInfo_validate))
		          {
		        	  userInfo.setPassword(userInfoService.encryptPassword(changePasswordForm.getConPassword()));
		        	  userInfo.setModifiedBy(userInfo.getUsername());
		        	  Date date = new Date();
		        	  userInfo.setLastChangedPassword(date);
		        	  userInfo.setLastLoggedOn(date);
		        	  userInfoService.update(userInfo);
			          changePasswordForm.setPassword_Changed("YES");
			          ActionMessages message = new ActionMessages();
			          message.add("result", new ActionMessage("changePassword.success"));
			          saveMessages(request, message);
			          changePasswordForm.setPassword("");
			          changePasswordForm.setOldPassword("");
			          changePasswordForm.setConPassword("");
			          if("Y".equalsIgnoreCase(changePasswordForm.getLogout()))
			          {
			        	  return(mapping.findForward("successlogOut"));
			          }
			          else
			          {			        	  
			        	  return mapping.findForward("success");
			          }
		          }
		          else
		          {
		        	  ActionMessages actionMessages = new ActionMessages();
		        	  changePasswordForm.setPassword_Changed("NO");
		        	  actionMessages.add("error", new ActionMessage("changePassword.unsuccessful"));
		        	  saveErrors(request, actionMessages);
		        	  if(userInfo.getLastLoggedOn()== null || "".equals(userInfo.getLastLoggedOn())){
		        		  return mapping.findForward("firstLogin");
		        	  }
		              return(mapping.findForward("success"));
		          }
		    }
		    catch (Exception e) {
				logger.error(e, e);
			}
	    }
	    return(mapping.findForward("error"));
	}
}
